<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="停车场编号" prop="parkId">
          <el-input v-model="dataForm.parkId" placeholder="停车场编号" disabled></el-input>
      </el-form-item>
          <el-form-item label="车位编号" prop="parkingId">
          <el-input v-model="dataForm.parkingId" placeholder="车位编号" disabled></el-input>
      </el-form-item>
          <el-form-item label="业主编号" prop="ownerId">
          <el-input v-model="dataForm.ownerId" placeholder="业主编号"></el-input>
      </el-form-item>
      <el-form-item label="使用到期时间" prop="endtime">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="dataForm.endtime"
                type="datetime"
                placeholder="使用到期时间">
            </el-date-picker>
          </div>



<!--          <el-input v-model="dataForm.endtime" placeholder="使用到期时间"></el-input>-->
      </el-form-item>
          <el-form-item label="更新人" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="更新人"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        parkId: '',
        parkingId: '',
        ownerId: '',
        starttime: '',
        endtime: '',
        creator: '',
        createTime: '',
        updator: '',
        updateTime: ''
      },
    }
  },
  computed: {
    dataRule () {
      return {
        // ownerId: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // starttime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // endtime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // createTime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // updator: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        // updateTime: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ]
      }
    }
  },
  methods: {

    initadd(item){
      console.log(item)
      this.visible = true
      this.dataForm.parkId=item.parkId;
      this.dataForm.parkingId=item.parkingId;
    },

    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/eightapp/eiuseparkinginfo/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      console.log(this.dataForm.endtime)
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/eightapp/eiuseparkinginfo/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
